<template>
    <div>
        <list class="scroller" v-if="bookListTemp" :style="{height:listHeight + 'px', width:listWidth+'px'}">
            <cell class="book-wrap row" @longpress="longpress(item)" v-for="(item,index) in bookListTemp"
                  @click="goToBook(item._id)" v-if="item">
                <div class="book-image">
                    <image class="image" :src="item.cover | imgFilter"></image>
                </div>
                <div class="book-info">
                    <text class="title margin-bottom">{{item.title}}</text>
                    <div class="other-info row margin-bottom">
                        <div class="other-info-left row">
                            <image class="author-image" :src="author_icon"></image>
                            <text class="text line1">{{item.author}}</text>
                        </div>
                    </div>
                    <div class="other-info row margin-bottom">
                        <div class="other-info-left row">
                            <text class="text">最新章节:</text>
                            <text class="text line1">{{item.lastChapter}}</text>
                        </div>
                    </div>
                </div>
            </cell>
        </list>
        <wxc-popup popup-color="#F3F3F3"
                   :show="isBottomShow"
                   @wxcPopupOverlayClicked="popupOverlayBottomClick"
                   pos="bottom"
                   :height="popupHeight">
            <div class="demo-content" v-if="selectBook">
                <div class="row center">
                    <text class="selectText">{{selectBook.title}}</text>
                </div>
                <div @click="goToBookInfo" class="row center">
                    <text class="selectText">详情</text>
                </div>
                <div @click="goToRead" class="row center">
                    <text class="selectText">阅读</text>
                </div>
                <div @click="deleteBook" class="row center">
                    <text class="selectText">删除</text>
                </div>
                <div @click="download" class="row center">
                    <text class="selectText">下载</text>
                </div>
            </div>
        </wxc-popup>
    </div>
</template>

<script>
    const modal = weex.requireModule('modal');
    import {author_icon, book_default_img, getImageUrl} from '../../baseConfig';
    import {WxcPopup} from 'weex-ui';
    import API from '../../util/api'
    import {createQuery} from '../../store/actions'
    import {toDownFile} from '../../register/register'
    import {Utils} from 'weex-ui'

    export default {
        name: "book-list",
        components: {WxcPopup},
        props: {
            bookList: {
                type: [Array, Object]
            },
            author_icon: {
                type: String,
                default: author_icon
            },
            loading: {
                type: Boolean,
                default: false
            },
            listHeight: {
                type: Number,
                default: 1344
            },
            listWidth: {
                type: Number,
                default: 750
            },
        },
        data() {
            return {
                book_default_img: book_default_img,
                isBottomShow: false,
                selectBook: {},
                popupHeight: 700,
            }
        },
        computed: {
            bookListTemp: {

                set: function (value) {
                    this.bookList = value;
                },
                get: function () {
                    return this.bookList;
                }

            }

        },
        methods: {
            goToBook(_id) {
                this.selectBook._id = _id;
                this.goToRead()
            },
            onLoading(event) {
                this.$emit('onLoading', event);
            },
            onAppear(index) {
                this.$emit('changDefaultCover', {index: index});
            },
            setImgSrc(img) {
                return img;
            },
            longpress(item) {
                this.isBottomShow = true;
                this.selectBook = item;
            },
            popupOverlayBottomClick() {
                this.isBottomShow = false;
            },
            goToBookInfo() {
                this.$router.push({'name': 'bookInfo', query: {bookId: this.selectBook._id}});
            },
            goToRead() {
                this.$router.push({name: 'read', query: {bookId: this.selectBook._id}});
            },
            deleteBook() {
                this.$emit('deleteBook', {bookId: this.selectBook._id});
                this.popupOverlayBottomClick();
            },
            download() {
                let params = {
                    bookId: this.selectBook._id
                }
                toDownFile(API.DOWNLOAD_BOOK + createQuery(params))
            }
        },
        filters: {
            imgFilter: getImageUrl
        }
    }
</script>

<style scoped>

    .row {
        flex-direction: row;
    }

    .desc,
    .author,
    .keep,
    .popularity {
        font-size: 24px;
        color: #666666;
    }

    .book-wrap {
        padding: 20px;
        font-size: 12px;
    }

    .book-info {
        flex: 2;
        padding: 10px 20px 0 16px;
    }

    .margin-bottom {
        margin-bottom: 20px;
    }

    .image {
        width: 182px;
        height: 230px;
    }

    .book-info {
        flex: 2;
    }

    .title {
        font-weight: 400;
        font-size: 32px;
    }

    .desc {
        lines: 2;
    }

    .other-info {
        justify-content: space-between;
    }

    .other-info-left, .other-info-right {
        justify-content: center;
        align-items: center;
    }

    .author-image {
        width: 26px;
        height: 26px;
        margin-right: 5px;
    }

    .author {
    }

    .popularity, .keep {
        border-style: solid;
        border-width: 1px;
        justify-content: center;
        align-items: center;
        padding: 5px;
    }

    /* 人气 */
    .popularity {
        color: #d81e06;
        border-color: #d81e06;
        margin-right: 10px;
    }

    .line1 {
        lines: 1;
    }

    .text {
        font-size: 24px;
    }

    .center {
        flex: 1;
        justify-content: center
    }

    .selectText {
        padding-left: 50px;
        padding-right: 20px;
        padding-top: 20px;
        padding-bottom: 20px;
        font-size: 36px;
    }
</style>